<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="industrialFormRules" :model="industrialForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="industrialForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="industrialForm.title" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="industrialForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        保存并发布
      </el-button>
      <el-button v-if="industrialForm.status.value <= 5" type="primary" @click="draftData()">
        保存
      </el-button>
      <el-button v-if="industrialForm.status.value === 5 && industrialForm.ID > 0" type="danger" @click="handleDelete(industrialForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveIndustrial, removeIndustrialByID, getIndustrialDetail } from '@/api/knowledge/industrial'
import { getIndustrialUsable } from '@/api/knowledge/industrial'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      industrialForm: {
        code: '',
        title: '',
        parentIndustrialID:0,
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      industrialOptions:[],
      industrialFormRules: []
    }
  },
  created(options) {
    this.loadEmployeeList()
    let parentIndustrialID = this.$route.query && this.$route.query.parentIndustrialID
    if(parentIndustrialID != undefined){
      this.industrialForm.parentIndustrialID = parentIndustrialID
    }
    let industrialID = this.$route.query && this.$route.query.industrialID
    if (industrialID !== undefined) {
      industrialID = parseInt(industrialID)
      if (!isNaN(industrialID)) {
        this.listLoading = true
        const formData = {
          'industrialID': industrialID
        }
        getIndustrialDetail(formData).then(response => {
          if (response.status === 200) {
            this.industrialForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(industrialID)
  },
  methods: {
    loadEmployeeList() {
      getIndustrialUsable().then(response => {
        if (response.status === 200) {
          this.industrialOptions = response.data.list
        }
      })
    },
    handleCannel() {
      this.$router.push({ path: 'industrial' })
    },
    draftData() {
      this.listLoading = true
      const _industrialForm = this.industrialForm
      _industrialForm.status = 1
      if (_industrialForm.ID > 0) {
        _industrialForm.ID = _industrialForm.ID
      }
      saveIndustrial(_industrialForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'industrial' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    createData() {
      this.listLoading = true
      const _industrialForm = this.industrialForm
      _industrialForm.status = 6
      if (_industrialForm.ID > 0) {
        _industrialForm.ID = _industrialForm.ID
      }
      saveIndustrial(_industrialForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'industrial' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'industrialID': row.ID
      }
      removeIndustrialByID(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'industrial' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
